<template>
    <div>
      <h2> 首页 </h2>
      <button @click="btn">axios</button>
      <button @click="excel">excel</button>
      <router-link to="/store">store</router-link>
      <router-link to="/" replace>login</router-link>
      <router-view style=" border: 1px solid red"/>
    </div>
</template>

<script>
    export default {
        name: "Home",
        mounted() {
          console.log('route.query ----');
          console.log(this.$route.query);
          console.log("this.$router.routes",this.$router);
          console.log("this.$router.routes",this.$router.options.routes);
        },
      methods:{
        btn(){
          this.$axios.get("/user").then((res)=>{
            console.log(res);
          })
        },
        excel(){
            this.$axios.post('/excel', {}, {
              //设置响应的数据类型为一个包含二进制数据的 Blob 对象，必须设置！！！
              responseType: 'blob'
            }).then(function (response) {
              /*const blob = new Blob([response.data]);*/
              console.log(response)
              const blob = new Blob([response],{type: 'application/vnd.ms-excel'})
              const fileName = 'xxx.xls';
              const linkNode = document.createElement('a');

              linkNode.download = fileName; //a标签的download属性规定下载文件的名称
              linkNode.style.display = 'none';
              linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
              document.body.appendChild(linkNode);
              linkNode.click();  //模拟在按钮上的一次鼠标单击

              URL.revokeObjectURL(linkNode.href); // 释放URL 对象
              document.body.removeChild(linkNode);

            }).catch(function (error) {
              console.log(error);
            });
        }
      }

    }
</script>

<style scoped>

</style>
